package cn.iocoder.yudao.module.system.controller.admin.gugu.gugu;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 高校基础信息实体类
 */
@Schema(description = "高校基础信息")
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class CollegeInfo {

    @JsonProperty("DataId")
    private String dataId;

    @JsonProperty("SchoolUUID")
    private String schoolUUID;

    @JsonProperty("CollegeName")
    private String collegeName;

    @JsonProperty("Province")
    private String province;

    @JsonProperty("City")
    private String city;

    @JsonProperty("District")
    private String district;

    @JsonProperty("Coordinate")
    private String coordinate;

    @JsonProperty("CollegeType")
    private String collegeType;

    @JsonProperty("Is985")
    private Boolean is985;

    @JsonProperty("Is211")
    private Boolean is211;

    @JsonProperty("IsDualClass")
    private Boolean isDualClass;

    @JsonProperty("CollegeCategory")
    private String collegeCategory;

    @JsonProperty("CollegeTags")
    private String collegeTags;

    /**
     * 获取解析后的学院标签列表
     * @return 学院标签列表
     */
    public List<String> getCollegeTagsList() {
        if (collegeTags == null || collegeTags.isEmpty() || collegeTags.equals("[]")) {
            return new ArrayList<>();
        }
        try {
            // 处理字符串形式的JSON数组，如 "[985高校, 211高校, 双一流, 教育部直属]"
            if (collegeTags.startsWith("[") && collegeTags.endsWith("]")) {
                String content = collegeTags.substring(1, collegeTags.length() - 1);
                if (content.isEmpty()) {
                    return new ArrayList<>();
                }
                String[] tags = content.split(",");
                List<String> result = new ArrayList<>();
                for (String tag : tags) {
                    // 去除可能的空格和引号
                    String trimmed = tag.trim();
                    if (trimmed.startsWith("\"") && trimmed.endsWith("\"")) {
                        trimmed = trimmed.substring(1, trimmed.length() - 1);
                    }
                    if (!trimmed.isEmpty()) {
                        result.add(trimmed);
                    }
                }
                return result;
            }
            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    @JsonProperty("EduLevel")
    private String eduLevel;

    @JsonProperty("CollegeProperty")
    private String collegeProperty;

    @JsonProperty("CollegeCode")
    private String collegeCode;

    @JsonProperty("Ranking")
    private Integer ranking;

    @JsonProperty("RankingInCategory")
    private String rankingInCategory;

    @JsonProperty("WebSite")
    private String webSite;

    @JsonProperty("CallNumber")
    private String callNumber;

    @JsonProperty("Email")
    private String email;

    @JsonProperty("Address")
    private String address;

    @JsonProperty("BranchList")
    private List<Branch> branchList;

    /**
     * 分校区信息
     */
    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Branch {
        @JsonProperty("BranchName")
        private String branchName;

        @JsonProperty("BranchAddress")
        private String branchAddress;
    }

    /**
     * 获取分校区字符串列表（用于数据库存储）
     * @return 分校区字符串列表
     */
    public List<String> getBranchListAsStrings() {
        if (branchList == null || branchList.isEmpty()) {
            return new ArrayList<>();
        }

        List<String> stringList = new ArrayList<>();
        for (Branch branch : branchList) {
            if (branch != null) {
                // 将分校区信息格式化为字符串，格式：分校区名称 - 地址
                String branchInfo = "";
                if (branch.getBranchName() != null && !branch.getBranchName().isEmpty()) {
                    branchInfo = branch.getBranchName();
                }
                if (branch.getBranchAddress() != null && !branch.getBranchAddress().isEmpty()) {
                    if (!branchInfo.isEmpty()) {
                        branchInfo += " - " + branch.getBranchAddress();
                    } else {
                        branchInfo = branch.getBranchAddress();
                    }
                }
                if (!branchInfo.isEmpty()) {
                    stringList.add(branchInfo);
                }
            }
        }
        return stringList;
    }

    @JsonProperty("CoverImage")
    private String coverImage;

    @JsonProperty("Intro")
    private String intro;

    @JsonProperty("Expenses")
    private String expenses;

    @JsonProperty("OldName")
    private String oldName;

    @JsonProperty("ShortName")
    private String shortName;

    @JsonProperty("MajorList")
    private List<MajorCategory> majorList;

    @JsonProperty("AffiliatedTo")
    private String affiliatedTo;

    @JsonProperty("IsMinistryOfEducationDirectlyAffiliated")
    private Boolean isMinistryOfEducationDirectlyAffiliated;

    @JsonProperty("NumMaster")
    private Integer numMaster;

    @JsonProperty("NumDoctor")
    private Integer numDoctor;

    @JsonProperty("GraduateRecommendationQualificationLevel")
    private Integer graduateRecommendationQualificationLevel;

    @JsonProperty("IsDeleted")
    private Boolean isDeleted;

    /**
     * 专业大类
     */
    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class MajorCategory {
        @JsonProperty("MajorTitle")
        private String majorTitle;

        @JsonProperty("Majors")
        private List<String> majors;
    }
}
